home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / program / tcpdumpb.zip / libpcap / grammar.y < prev    next >
Text File  |  1996-07-17  |  7KB  |  282 lines

  1. %{
  2. /*
  3.  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
  4.  *    The Regents of the University of California.  All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that: (1) source code distributions
  8.  * retain the above copyright notice and this paragraph in its entirety, (2)
  9.  * distributions including binary code include the above copyright notice and
  10.  * this paragraph in its entirety in the documentation or other materials
  11.  * provided with the distribution, and (3) all advertising materials mentioning
  12.  * features or use of this software display the following acknowledgement:
  13.  * ``This product includes software developed by the University of California,
  14.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  15.  * the University nor the names of its contributors may be used to endorse
  16.  * or promote products derived from this software without specific prior
  17.  * written permission.
  18.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  19.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  20.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  *
  22.  */
  23. #ifndef lint
  24. static char rcsid[] =
  25.     "@(#) $Header: grammar.y,v 1.54 96/07/17 00:11:34 leres Exp $ (LBL)";
  26. #endif
  27.  
  28. #include <sys/types.h>
  29. #include <sys/time.h>
  30. #include <sys/socket.h>
  31.  
  32. #if __STDC__
  33. struct mbuf;
  34. struct rtentry;
  35. #endif
  36.  
  37. #include <net/if.h>
  38.  
  39. #include <netinet/in.h>
  40. #include <netinet/if_ether.h>
  41.  
  42. #include <stdio.h>
  43.  
  44. #include "pcap-int.h"
  45.  
  46. #include "gencode.h"
  47. #include <pcap-namedb.h>
  48.  
  49. #include "gnuc.h"
  50. #ifdef HAVE_OS_PROTO_H
  51. #include "os-proto.h"
  52. #endif
  53.  
  54. #define QSET(q, p, d, a) (q).proto = (p),\
  55.              (q).dir = (d),\
  56.              (q).addr = (a)
  57.  
  58. int n_errors = 0;
  59.  
  60. static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
  61.  
  62. static void
  63. yyerror(char *msg)
  64. {
  65.     ++n_errors;
  66.     bpf_error(msg);
  67.     /* NOTREACHED */
  68. }
  69.  
  70. #ifndef YYBISON
  71. int yyparse(void);
  72.  
  73. int
  74. pcap_parse()
  75. {
  76.     return (yyparse());
  77. }
  78. #endif
  79.  
  80. %}
  81.  
  82. %union {
  83.     int i;
  84.     bpf_u_int32 h;
  85.     u_char *e;
  86.     char *s;
  87.     struct stmt *stmt;
  88.     struct arth *a;
  89.     struct {
  90.         struct qual q;
  91.         struct block *b;
  92.     } blk;
  93.     struct block *rblk;
  94. }
  95.  
  96. %type    <blk>    expr id nid pid term rterm qid
  97. %type    <blk>    head
  98. %type    <i>    pqual dqual aqual ndaqual
  99. %type    <a>    arth narth
  100. %type    <i>    byteop pname pnum relop irelop
  101. %type    <blk>    and or paren not null prog
  102. %type    <rblk>    other
  103.  
  104. %token  DST SRC HOST GATEWAY
  105. %token  NET MASK PORT LESS GREATER PROTO BYTE
  106. %token  ARP RARP IP TCP UDP ICMP IGMP IGRP
  107. %token  ATALK DECNET LAT SCA MOPRC MOPDL
  108. %token  TK_BROADCAST TK_MULTICAST
  109. %token  NUM INBOUND OUTBOUND
  110. %token  LINK
  111. %token    GEQ LEQ NEQ
  112. %token    ID EID HID
  113. %token    LSH RSH
  114. %token  LEN
  115.  
  116. %type    <s> ID
  117. %type    <e> EID
  118. %type    <s> HID
  119. %type    <i> NUM
  120.  
  121. %left OR AND
  122. %nonassoc  '!'
  123. %left '|'
  124. %left '&'
  125. %left LSH RSH
  126. %left '+' '-'
  127. %left '*' '/'
  128. %nonassoc UMINUS
  129. %%
  130. prog:      null expr
  131. {
  132.     finish_parse($2.b);
  133. }
  134.     | null
  135.     ;
  136. null:      /* null */        { $$.q = qerr; }
  137.     ;
  138. expr:      term
  139.     | expr and term        { gen_and($1.b, $3.b); $$ = $3; }
  140.     | expr and id        { gen_and($1.b, $3.b); $$ = $3; }
  141.     | expr or term        { gen_or($1.b, $3.b); $$ = $3; }
  142.     | expr or id        { gen_or($1.b, $3.b); $$ = $3; }
  143.     ;
  144. and:      AND            { $$ = $<blk>0; }
  145.     ;
  146. or:      OR            { $$ = $<blk>0; }
  147.     ;
  148. id:      nid
  149.     | pnum            { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
  150.                            $$.q = $<blk>0.q); }
  151.     | paren pid ')'        { $$ = $2; }
  152.     ;
  153. nid:      ID            { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
  154.     | HID '/' NUM        { $$.b = gen_mcode($1, NULL, $3,
  155.                     $$.q = $<blk>0.q); }
  156.     | HID MASK HID        { $$.b = gen_mcode($1, $3, 0,
  157.                     $$.q = $<blk>0.q); }
  158.     | HID            {
  159.                   /* Decide how to parse HID based on proto */
  160.                   $$.q = $<blk>0.q;
  161.                   switch ($$.q.proto) {
  162.                   case Q_DECNET:
  163.                     $$.b = gen_ncode($1, 0, $$.q);
  164.                     break;
  165.                   default:
  166.                     $$.b = gen_ncode($1, 0, $$.q);
  167.                     break;
  168.                   }
  169.                 }
  170.     | EID            { $$.b = gen_ecode($1, $$.q = $<blk>0.q); }
  171.     | not id        { gen_not($2.b); $$ = $2; }
  172.     ;
  173. not:      '!'            { $$ = $<blk>0; }
  174.     ;
  175. paren:      '('            { $$ = $<blk>0; }
  176.     ;
  177. pid:      nid
  178.     | qid and id        { gen_and($1.b, $3.b); $$ = $3; }
  179.     | qid or id        { gen_or($1.b, $3.b); $$ = $3; }
  180.     ;
  181. qid:      pnum            { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
  182.                            $$.q = $<blk>0.q); }
  183.     | pid
  184.     ;
  185. term:      rterm
  186.     | not term        { gen_not($2.b); $$ = $2; }
  187.     ;
  188. head:      pqual dqual aqual    { QSET($$.q, $1, $2, $3); }
  189.     | pqual dqual        { QSET($$.q, $1, $2, Q_DEFAULT); }
  190.     | pqual aqual        { QSET($$.q, $1, Q_DEFAULT, $2); }
  191.     | pqual PROTO        { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
  192.     | pqual ndaqual        { QSET($$.q, $1, Q_DEFAULT, $2); }
  193.     ;
  194. rterm:      head id        { $$ = $2; }
  195.     | paren expr ')'    { $$.b = $2.b; $$.q = $1.q; }
  196.     | pname            { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
  197.     | arth relop arth    { $$.b = gen_relation($2, $1, $3, 0);
  198.                   $$.q = qerr; }
  199.     | arth irelop arth    { $$.b = gen_relation($2, $1, $3, 1);
  200.                   $$.q = qerr; }
  201.     | other            { $$.b = $1; $$.q = qerr; }
  202.     ;
  203. /* protocol level qualifiers */
  204. pqual:      pname
  205.     |            { $$ = Q_DEFAULT; }
  206.     ;
  207. /* 'direction' qualifiers */
  208. dqual:      SRC            { $$ = Q_SRC; }
  209.     | DST            { $$ = Q_DST; }
  210.     | SRC OR DST        { $$ = Q_OR; }
  211.     | DST OR SRC        { $$ = Q_OR; }
  212.     | SRC AND DST        { $$ = Q_AND; }
  213.     | DST AND SRC        { $$ = Q_AND; }
  214.     ;
  215. /* address type qualifiers */
  216. aqual:      HOST            { $$ = Q_HOST; }
  217.     | NET            { $$ = Q_NET; }
  218.     | PORT            { $$ = Q_PORT; }
  219.     ;
  220. /* non-directional address type qualifiers */
  221. ndaqual:  GATEWAY        { $$ = Q_GATEWAY; }
  222.     ;
  223. pname:      LINK            { $$ = Q_LINK; }
  224.     | IP            { $$ = Q_IP; }
  225.     | ARP            { $$ = Q_ARP; }
  226.     | RARP            { $$ = Q_RARP; }
  227.     | TCP            { $$ = Q_TCP; }
  228.     | UDP            { $$ = Q_UDP; }
  229.     | ICMP            { $$ = Q_ICMP; }
  230.     | IGMP            { $$ = Q_IGMP; }
  231.     | IGRP            { $$ = Q_IGRP; }
  232.     | ATALK            { $$ = Q_ATALK; }
  233.     | DECNET        { $$ = Q_DECNET; }
  234.     | LAT            { $$ = Q_LAT; }
  235.     | SCA            { $$ = Q_SCA; }
  236.     | MOPDL            { $$ = Q_MOPDL; }
  237.     | MOPRC            { $$ = Q_MOPRC; }
  238.     ;
  239. other:      pqual TK_BROADCAST    { $$ = gen_broadcast($1); }
  240.     | pqual TK_MULTICAST    { $$ = gen_multicast($1); }
  241.     | LESS NUM        { $$ = gen_less($2); }
  242.     | GREATER NUM        { $$ = gen_greater($2); }
  243.     | BYTE NUM byteop NUM    { $$ = gen_byteop($3, $2, $4); }
  244.     | INBOUND        { $$ = gen_inbound(0); }
  245.     | OUTBOUND        { $$ = gen_inbound(1); }
  246.     ;
  247. relop:      '>'            { $$ = BPF_JGT; }
  248.     | GEQ            { $$ = BPF_JGE; }
  249.     | '='            { $$ = BPF_JEQ; }
  250.     ;
  251. irelop:      LEQ            { $$ = BPF_JGT; }
  252.     | '<'            { $$ = BPF_JGE; }
  253.     | NEQ            { $$ = BPF_JEQ; }
  254.     ;
  255. arth:      pnum            { $$ = gen_loadi($1); }
  256.     | narth
  257.     ;
  258. narth:      pname '[' arth ']'        { $$ = gen_load($1, $3, 1); }
  259.     | pname '[' arth ':' NUM ']'    { $$ = gen_load($1, $3, $5); }
  260.     | arth '+' arth            { $$ = gen_arth(BPF_ADD, $1, $3); }
  261.     | arth '-' arth            { $$ = gen_arth(BPF_SUB, $1, $3); }
  262.     | arth '*' arth            { $$ = gen_arth(BPF_MUL, $1, $3); }
  263.     | arth '/' arth            { $$ = gen_arth(BPF_DIV, $1, $3); }
  264.     | arth '&' arth            { $$ = gen_arth(BPF_AND, $1, $3); }
  265.     | arth '|' arth            { $$ = gen_arth(BPF_OR, $1, $3); }
  266.     | arth LSH arth            { $$ = gen_arth(BPF_LSH, $1, $3); }
  267.     | arth RSH arth            { $$ = gen_arth(BPF_RSH, $1, $3); }
  268.     | '-' arth %prec UMINUS        { $$ = gen_neg($2); }
  269.     | paren narth ')'        { $$ = $2; }
  270.     | LEN                { $$ = gen_loadlen(); }
  271.     ;
  272. byteop:      '&'            { $$ = '&'; }
  273.     | '|'            { $$ = '|'; }
  274.     | '<'            { $$ = '<'; }
  275.     | '>'            { $$ = '>'; }
  276.     | '='            { $$ = '='; }
  277.     ;
  278. pnum:      NUM
  279.     | paren pnum ')'    { $$ = $2; }
  280.     ;
  281. %%
  282.